<?php
/**
 * @author Pham Van Diep
 * @date: 18/02/2014
 * @description: save update club
 */
// not login
// not perm ad
if(!isset($AppUI->id) || 
	! isset($_SESSION['club'])||
	($_SESSION['club']&1<<2) == 0	
) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}

// data input
$intClubId          = $this->input->post('clubid');
$strName            = CI_Function::stripTag(trim($this->input->post('clubname')));
$intLen = strlen($strName);
if($intLen <= VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
	array_push($arrErr,$arrLocale['name_range']);
}
$strNickName        = CI_Function::stripTag(trim($this->input->post('clubnickname',null)));
$intLen = strlen($strNickName);
if($intLen <= VL_NICKNAME_MINLENGTH || $intLen > VL_NICKNAME_MAXLENGTH ) {
	array_push($arrErr,$arrLocale['nickname_range']);
}
$strCountry         = CI_Function::stripTag(trim($this->input->post('clubcountry',null)));
$intLen = strlen($strCountry);
if($intLen != VL_COUNTRY_LENGTH ) {
	array_push($arrErr,$arrLocale['country_condition']);
}

$strLogo            = '';
if( isset($_FILES['clublogo']) ) {
    $this->load->library('upload');
    $strLogo = $this->upload->do_upload('clublogo');
    $strLogo = $strLogo['path'];
    $this->load->model('image_model','image');
    //$this->image->add();
}
$strSlug            = CI_Function::stripTag(trim($this->input->post('clubslug', null)));
$strShort           = CI_Function::stripTag(trim($this->input->post('clubshort',null)));
$strDescription     = CI_Function::stripTag(trim($this->input->post('clubdescription',null)));
$strTag             = CI_Function::stripTag(trim($this->input->post('clubtag',null)));
$arrErr             = array();
if(isset($arrErr) && count($arrErr) == 0) {//allow save  
	if(isset($_SESSION['CLUB_ERROR'])) unset($_SESSION['CLUB_ERROR']);
	if(isset($_SESSION['CLUB_INPUT'])) unset($_SESSION['CLUB_INPUT']);
    $ret = $this->club->update($AppUI->id,$intClubId,$strName,$strNickName,$strCountry ,$strLogo ,$strSlug ,$strShort,$strDescription, $strTag );
    if($ret == 0) CI_Function::setMsg('Update successfully');
    else if($ret == -1) array_push($arrErr, $arrError['system_error']);
    else array_push($arrErr, $arrError['problem_save_db_error']) ;   
}
// occur error

if (isset($arrErr) && count($arrErr) > 0) { 
	$_SESSION['CLUB_ERROR'] = $arrErr;      
   redirect(HOST_BACKEND . 'club/edit?clubid=' . $intClubId );
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
